YouTube Clone

YouTube and Clone web sites

How to convert/encode files to FLV using FFMPEG & PHP

So, as I’ve written in an earlier article on how to install FFMPEG on your server, while there are those who probably use a “YouTube Clone” script, there might be those who want to create their own using FFMPEG & PHP. FLV is the most widely used type of codec that runs on most Flash players.

So, let’s get started, there are actually a few steps into converting a file to FLV which are shown below

1. Send the script to FFMPEG-PHP and get it’s info
So, before doing any of this, you should make sure that your file has been uploaded to somewhere and you have the full path to it. (You can’t use what you have in “memory”, so you’ll have to look on how to upload a file, once you got that and have the path of the file, we’ll start our script to invoke FFMPEG-PHP and get the file’s resolution. What we mainly need is the width, height & FPS (frame per second) so that we can tell FFMPEG about. I’ll be using the clock.avi located in every windows system.

We’ll start out our code with getting our variables:

// Set our source file
$srcFile = "/path/to/clock.avi";
$destFile = "/path/to/clock.flv";
$ffmpegPath = "/path/to/ffmpeg";
$flvtool2Path = "/path/to/flvtool2";

// Create our FFMPEG-PHP class
$ffmpegObj = new ffmpeg_movie($srcFile);

// Save our needed variables
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();

Also, the width/height has to be multiples of two so I have created a function that makes it a multiple of two:

function makeMultipleTwo ($value)
{
$sType = gettype($value/2);

if($sType == “integer”)
{
return $value;
} else {
return ($value-1);
}
}

2. Send the script to FFMPEG for encoding
Here is where the fun starts, executing it and telling FFMPEG where to place it later. Let’s see on how our command will consist and what it will be made of. We’ll see what quality settings we will have to set.

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv

That’s generally how to convert a video.avi to video.flv with the audio sampling at 22050 & audio bit rate at 32, with the size 320×240. While I suggest the values above for audio as they are the most compressed, but we’ll use the old audio settings for better quality.

$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();

Now we have pretty much most of the values ready for our compression, however, we need to call flvtool2 to get our Meta information. Steps 4 and 5 in the diagram work simultaneously with this one.

What we do is make flvtool2 run at the same time as FFMPEG so we’ll pipe it into the command which means our general command is

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv | flvtool2 -U stdin video.flv

Now, we have a kind of complete command, let’s make our final code!

<?php
// Set our source file
$srcFile = "/path/to/clock.avi";
$destFile = "/path/to/clock.flv";
$ffmpegPath = "/path/to/ffmpeg";
$flvtool2Path = "/path/to/flvtool2";
// Create our FFMPEG-PHP class
$ffmpegObj = new ffmpeg_movie($srcFile);
// Save our needed variables
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();
// Call our convert using exec()
exec($ffmpegPath . " -i " . $srcFile . " -ar " . $srcAR . " -ab " . $srcAB . " -f flv -s " . $srcWidth . "x" . $srcHeight . " " . $destFile . " | " . $flvtool2Path . " -U stdin " . $destFile);
// Make multiples function
function makeMultipleTwo ($value)
{
$sType = gettype($value/2);
if($sType == "integer")
{
return $value;
} else {
return ($value-1);
}
}
?>

Source

May 26, 2007 - Posted by | video, youtube clone

13 Comments »

  1. Fatal error: Class ‘ffmpeg_movie’ not found !!!
    Am getting this error??

    Waht happen!!!

    Comment by Kidu mon | March 8, 2008 | Reply

  2. when we call ffmpeg.exe with exec() the process prevents the rest of the script to proceed, if you call pclose(popen(..)) instead of exec, conversion proceed at the background so the page responds to user just after the uploading of file completed.

    Comment by kasirgat | March 13, 2008 | Reply

  3. Hi,

    But i couldn’t convert the files of size >1MB…

    Same code as above…
    NOt a little change… HOsting in dreamhost..

    Any help from here…

    Comment by KiduMon | March 17, 2008 | Reply

  4. Not working can you please help me to get it done ??

    can’t we do without flvtool2 ?

    Comment by IT Outsourcing | March 20, 2008 | Reply

  5. @ Kidu mon

    You don’t have ffmpeg-php installed on the server. Check your phpinfo.

    @ KiduMon

    dreamhost have some process watch script running, that check each process and kill any process that use more cpu, memory or run for long. They do this to keep server stable for all shared users and disable one user take more than a specified amount of system resources they allow.

    @ IT Outsourcing

    flvtool is not needed for converting video to flv. It is for meta injection. For http streaming, it won’t make much difference.

    Comment by youtubeclone | March 20, 2008 | Reply

  6. I assume if I did not want to use FLVTool I change the exec line too

    exec($ffmpegPath . ” -i ” . $srcFile . ” -ar ” . $srcAR . ” -ab ” . $srcAB . ” -f flv -s ” . $srcWidth . “x” . $srcHeight . ” ” . $destFile . ”);

    Comment by Steve | April 7, 2008 | Reply

  7. I have managed to get the code working and it converts the clock.avi to clock.flv but the clock.flv is 0kb when encoded. What is the problem and why is this happening? Any ideas.

    Comment by sfi | April 18, 2008 | Reply

  8. This PHP Flash Video Script looks ok i’ll try it. and i’ll come back and post how it goes.

    Comment by mycoolny | May 8, 2008 | Reply

  9. Fatal error: Class ‘ffmpeg_movie’ not found in D:\wamp\www\uploadvideo\encoder.php on line 8

    Comment by revathi | May 8, 2008 | Reply

  10. […] How to convert/encode files to FLV using FFMPEG & PHP […]

    Pingback by Mi faccio lo youtube in casa | Napolux.com | May 9, 2008 | Reply

  11. Hello Friends,

    I am using ffmpeg to create thumbnail, and it works great … BUT THE ISSUE is with converting the avi file to flv… it create the .flv file but it has null content …

    Please help me I have stuck here form last 2 days … Any help will be greatly appreciated …

    Thanks in advance ,
    Justin

    Comment by Justin | May 23, 2008 | Reply

  12. I tested this and everything is setup on my dedicated server properly. When it converts it makes the file FLV file 0kb
    So its close to working but it does not work. Back to google… lol

    Comment by Lance Prevost | March 10, 2010 | Reply

  13. Iam also struck at converting an avi file… please help

    Comment by shashi kanth | January 24, 2011 | Reply


Leave a comment